﻿Ext.Loader.setConfig({enabled : true});
Ext.Loader.setPath('Ext.ux', 'extjs/ux/');
Ext.application ({
    name : "tuan",
    requires : ['tuan.config.Runtime'],
    controllers : ["LoginController","HomeController","KhachHangController","DonHangController","DashboardController"],
    views : [],
    appFolder : "app",
    launch : function(){
         _myAppGlobal = this;
        var checkLogin = sessionStorage.getItem('sessionUser');
        console.log(checkLogin);
        if(checkLogin === "yes"){
            this.validate_custom();
            this.viewport = Ext.create("Ext.container.Viewport",{
                    renderTo : Ext.getBody(),
                    layout : "card",
                    items : [
                        {xtype : "home"}
                    ]
            })
        }else{
            this.viewport = Ext.create("Ext.container.Viewport",{
                renderTo : Ext.getBody(),
                layout : "card",
                items : [
                    {xtype : "login"}
                ]
            })
        }
    },
    runaction : function (controllername, actionname){
        var controller = this.getController(controllername);
        //controller.init();
        controller['action'+actionname]();
    },
    validate_custom : function(){
        Ext.apply(Ext.form.field.VTypes, {
            daterange: function(val, field) {
                var date = field.parseDate(val);
                if (!date) {
                    return false;
                }
                if (field.startDateField && (!this.dateRangeMax || (date.getTime() != this.dateRangeMax.getTime()))) {
                    var start = field.up('form').down('#' + field.startDateField);
                    start.setMaxValue(date);
                    start.validate();
                    this.dateRangeMax = date;
                }
                else if (field.endDateField && (!this.dateRangeMin || (date.getTime() != this.dateRangeMin.getTime()))) {
                    var end = field.up('form').down('#' + field.endDateField);
                    end.setMinValue(date);
                    end.validate();
                    this.dateRangeMin = date;
                }
                /*
                    * Always return true since we're only using this vtype to set the
                    * min/max allowed values (these are tested for after the vtype test)
                    */
                return true;
            },
            daterangeText: 'Start date must be less than end date',
        });
        var usernameTest = /^[A-Za-z]\w+$/;
         Ext.apply(Ext.form.field.VTypes, {
            //  vtype validation function
            username: function(val, field) {
                return usernameTest.test(val);
            },
            // vtype Text property: The error text to display when the validation function returns false
            usernameText: "Tên không được chứa khoảng trắng hay ký tự đặc biệt, chấp nhận '_'",
            // vtype Mask property: The keystroke filter mask
            usernameMask: /[a-zA-Z \w]/
        });

        var telNumberVType = {
            telNumber: function(val, field){
                var telNumberRegex = /^\d{3,4}\-\d{3,4}\-\d{3,5}$/;
                return telNumberRegex.test(val);
            },
            telNumberText: 'Số điện thoại phải chứa toàn ký tự số và ký tự "-"',
            telNumberMask: /[\d\-]/
        };
        Ext.apply(Ext.form.field.VTypes, telNumberVType);
    }
})